﻿Imports System.ComponentModel
Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.Text
Imports Global_Visas_Business_Layer.Admin

Partial Public Class frmAdminManageAutomation

    Private Sub frmAdminManageAutomation_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub


    Private Sub frmAdminManageAutomation_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        GeneralTasks.ChangeTheme(Me)
        'GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        With drpFrom
            .DisplayMemberPath = "CaseStage_Title"
            .SelectedValuePath = "StageLink_FromID"
            .ItemsSource = CType(Admin.GetcaseStages(LblDiagramid.Content), IListSource).GetList
            .SelectedIndex = -1
        End With
        With drpTask
            .DisplayMemberPath = "TaskTemplate_Name"
            .SelectedValuePath = "TaskTemplate_ID"
            .ItemsSource = CType(CommonDataFunctions.TaskList, IListSource).GetList
            .SelectedIndex = -1
        End With
        refreshList(0, 0)
    End Sub

    Private Sub drpFrom_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpFrom.SelectionChanged
        If drpFrom.SelectedValue > 0 Then
            With drpTo
                .DisplayMemberPath = "CaseStage_Title"
                .SelectedValuePath = "StageLink_ToID"
                .ItemsSource = CType(Admin.GetcaseStages_To(drpFrom.SelectedValue, LblDiagramid.Content), IListSource).GetList
                .SelectedIndex = -1
            End With
        End If
        drpTask.IsEnabled = False
        drpTask.SelectedIndex = -1
        txtBody.Text = String.Empty
    End Sub

    Private Sub drpTo_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpTo.SelectionChanged
        drpTask.SelectedIndex = 0
        If drpFrom.SelectedValue > 0 And drpTo.SelectedValue > 0 Then
            drpTask.SelectedValue = Admin.GetTaskTemplateforcasestage(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content) 'task templete is to be created
            grdTemplates.ItemsSource = Admin.GetTaskTemplatesForCaseStage(drpFrom.SelectedValue, drpTo.SelectedValue)
            drpTask.IsEnabled = True
            If (drpTask.SelectedValue > 0) Then
                Dim taskInfo As TaskTemplateInfo = Admin.GetTaskInfoForManageAutomation(drpFrom.SelectedValue, drpTo.SelectedValue)
                txtWorkingDays.Text = taskInfo.TemplateWorkingDays
                txtBody.Text = taskInfo.Message
                btnSave.IsEnabled = False
            Else
                txtWorkingDays.Text = String.Empty
                txtBody.Text = String.Empty
            End If
        End If
    End Sub

    Private Sub drpTask_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpTask.SelectionChanged
        If Not drpTask.SelectedItem Is Nothing Then
            If drpTask.SelectedItem.row.itemarray(0).ToString > 0 Then
                Dim templateInfo As Admin.TaskTemplateInfo = Admin.GetTaskTemplateMessageFromID(drpTask.SelectedItem.row.itemarray(0).ToString)
                txtBody.Text = templateInfo.Message
                txtWorkingDays.Text = templateInfo.TemplateWorkingDays
                txtWorkingDays.IsEnabled = True
            End If
        End If
        btnSave.IsEnabled = True
        btnSetNothing.IsEnabled = True
        txtBody.IsEnabled = False
        'txtWorkingDays.IsEnabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If (String.IsNullOrEmpty(txtWorkingDays.Text)) Then
            CommonFuncs.DisplayMessage("The number of days cannot be empty.", CommonDialog.CustomDialogIcons.Warning, "Task Template")
        End If
        Dim TasksEvents As TasksEvents
        Try
            Dim workingDays As Integer = Convert.ToInt32(txtWorkingDays.Text)
            If Admin.UpdateTaskAutomation(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content, drpTask.SelectedValue, workingDays) = True Then
                refreshList(drpFrom.SelectedValue, drpTo.SelectedValue)
                TasksEvents = New TasksEvents
                TasksEvents.GblFunc_Add_SysEvent("Task Template", "The task template has been updated as " & drpTask.SelectedValue & " for the case stage - From : " & drpFrom.SelectedValue & " To : " & drpTo.SelectedValue & " and Diagramid : " & LblDiagramid.Content)
                CommonFuncs.DisplayMessage("The task template has been updated", CommonDialog.CustomDialogIcons.Information, "Task Template")
            End If
        Catch ex As Exception
        Finally
            TasksEvents = Nothing
        End Try
        txtWorkingDays.IsEnabled = False
        btnSetNothing.IsEnabled = False
        btnSave.IsEnabled = False
        btnEdit.IsEnabled = True
        txtBody.IsEnabled = False
        btnCancel.IsEnabled = False
        btnCancel.IsEnabled = False
    End Sub

    Private Sub btnSetNothing_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSetNothing.Click
        txtWorkingDays.Text = 0
        Dim TasksEvents As TasksEvents
        Try
            Dim workingDays As Integer = Convert.ToInt32(txtWorkingDays.Text)
            If Admin.UpdateTaskAutomation(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content, 0, workingDays) = True Then
                TasksEvents = New TasksEvents
                TasksEvents.GblFunc_Add_SysEvent("Task Template", "The task template has been updated as " & 0 & " for the case stage - From : " & drpFrom.SelectedValue & " To : " & drpTo.SelectedValue & " and Diagramid : " & LblDiagramid.Content)
                CommonFuncs.DisplayMessage("The task template has been set to nothing", CommonDialog.CustomDialogIcons.Information, "Task Template")
            End If
        Catch ex As Exception
        Finally
            TasksEvents = Nothing
        End Try
        btnEdit.IsEnabled = True
        btnSave.IsEnabled = False
        btnSetNothing.IsEnabled = False
        txtBody.IsEnabled = False
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        txtWorkingDays.IsEnabled = True
        txtBody.IsEnabled = True
        btnSave.IsEnabled = True
        btnSetNothing.IsEnabled = True
        txtWorkingDays.Focus()
    End Sub

    Private Sub grdTemplates_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdTemplates.SelectionChanged

        If Me.grdTemplates.SelectedItems.Count = 0 Then
            Me.grdTemplates.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdTemplates.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Dim selectedRow As DataRow = grdTemplates.SelectedItem
            If Not IsNothing(selectedRow) Then
                drpFrom.SelectedValue = Convert.ToInt32(selectedRow("stagefrom").ToString())
                With drpTo
                    .DisplayMemberPath = "CaseStage_Title"
                    .SelectedValuePath = "StageLink_ToID"
                    .ItemsSource = CType(Admin.GetcaseStages_To(drpFrom.SelectedValue, LblDiagramid.Content), IListSource).GetList
                    .SelectedValue = Convert.ToInt32(selectedRow("stageto").ToString())
                End With
                With drpTask
                    .DisplayMemberPath = "TaskTemplate_Name"
                    .SelectedValuePath = "TaskTemplate_ID"
                    .ItemsSource = CType(CommonDataFunctions.TaskList, IListSource).GetList
                    .SelectedValue = Convert.ToInt32(selectedRow("TemplateId").ToString())
                End With
                txtWorkingDays.Text = selectedRow("Working Days").ToString()
                'drpTask.SelectedValue = Admin.GetTaskTemplateforcasestage(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content) 'task templete is to be created
            End If
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete the mapping ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRow = grdTemplates.SelectedItem
            Dim stageFrom As Integer = Convert.ToInt32(selectedRow("stagefrom").ToString())
            Dim stageTo As Integer = Convert.ToInt32(selectedRow("stageto").ToString())
            Dim templateId As Integer = Convert.ToInt32(selectedRow("templateId").ToString())
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete FROM [L_StageLinksTaskTemplateMapping] where StageLink_FromId =" & stageFrom)
                .Append("  and StageLink_ToID =" & stageTo & " and TaskTemplate_ID =" & templateId)
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdTemplates.IsEnabled = True
                drpFrom.SelectedIndex = -1
                drpTo.SelectedIndex = -1
                drpTask.SelectedIndex = -1
                txtBody.Text = String.Empty
                txtWorkingDays.Text = String.Empty
                refreshList(0, 0)
                MsgBox("The Template Mapping has been deleted successfully.", MsgBoxStyle.Information, "Success")
            Else
                MsgBox("There is an error while deleting .Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If
    End Sub

    Private Sub refreshList(ByVal stageFrom As Integer, ByVal stageTo As Integer)
        If (stageFrom = -1 Or stageTo = -1) Then
            stageFrom = 0
            stageTo = 0
        End If
        grdTemplates.ItemsSource = Admin.GetTaskTemplatesForCaseStage(stageFrom, stageTo)
        Me.grdTemplates.Columns("TemplateId").IsVisible = False
        Me.grdTemplates.Columns("stagefrom").IsVisible = False
        Me.grdTemplates.Columns("stageTo").IsVisible = False
    End Sub
End Class
